import {message,Tabs,notification} from 'antd'
import React, { useState, useRef } from 'react'
import { PageHeaderWrapper } from '@ant-design/pro-layout'
import ProTable, { ProColumns, ActionType } from '@ant-design/pro-table'
import { employeesList,saveContractInfo } from '../service'
import moment from 'moment';
import { Link } from 'umi'
const  { TabPane } = Tabs;
import {contractStatusList} from "@/utils/constant";
import CreateForm from './components/CreateForm'

const TableList = (props) => {
  const actionRef = useRef()
  const actionRef2 = useRef()
  const [tabIndex, setTabIndex] = useState("0")
  const [visible, setVisible] = useState(null)
  const [record,setRecord] = useState(null)
  const [loading,setLoading] = useState(false)
  const columns = [
    {
      title: '编号',
      dataIndex: 'employeeOid',
      hideInSearch: true,
      render: (item, record) => (
        <Link to={`/info/details/${record.oid}`}>{item}</Link>
      ),
    },
    {
      title: '姓名',
      dataIndex: 'name',
    },
    {
      title: '公司名称',
      dataIndex: 'orgname',
    },
    {
      title: '部门名称',
      dataIndex: 'unitname',
    },
    {
      title: '岗位名称',
      dataIndex: 'jobname',
    },
    {
      title: '是否复职',
      dataIndex: 'isReinstate',
      valueEnum: {
        0: { text: '否' },
        1: { text: '是' },
        3: { text: '是' },
      },
      hideInSearch: true
    },
    {
      title: '入职日期',
      dataIndex: 'hiredate',
      render: (text) => {
        return text ? moment(text).format('YYYY-MM-DD') : ''
      },
      hideInSearch: true,
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (item, record) => (
        <>
          <a onClick={() => sendContract(record)}>发送合同</a>
        </>
      ),
    },
  ]
  const alreadyColumns = [
    {
      title: '编号',
      dataIndex: 'employeeOid',
      hideInSearch: true,
      render: (item, record) => (
        <Link to={`/info/details/${record.oid}`}>{item}</Link>
      ),
    },
    {
      title: '姓名',
      dataIndex: 'name',
    },
    {
      title: '公司名称',
      dataIndex: 'orgname',
    },
    {
      title: '部门名称',
      dataIndex: 'unitname',
    },
    {
      title: '岗位名称',
      dataIndex: 'jobname',
    },
    {
      title: '是否复职',
      dataIndex: 'isReinstate',
      valueEnum: {
        0: { text: '否' },
        1: { text: '是' },
        3: { text: '是' },
      },
      hideInSearch: true
    },
    {
      title: '入职日期',
      dataIndex: 'hiredate',
      render: (text) => {
        return text ? moment(text).format('YYYY-MM-DD') : ''
      },
      hideInSearch: true,
    },
    {
      title: '合同状态',
      dataIndex: 'isSendContract',
      render: (text) => {
        const res = contractStatusList.find((item) => item.value === text)
        return res ? res.label : text
      },
      hideInSearch: true,
    },
  ]

  const sendContract = (record) =>{
    setVisible(true);
    setRecord(record);
  }

  const handleCancel = () => {
    setVisible(false);
    setRecord(null);
  }

  const handleSubmit = (values) => {
    setLoading(true);
    saveContractInfo(values).then((res) => {
      if(res.flag){
        message.success('合同发送成功！');
        handleCancel();
        actionRef.current.reload()
      }else{
        notification.error({
          message: `请求错误 ${res.status}`,
          description: res.error,
        });
      }
      setLoading(false)
    })
  }

  const handleChangTabIndex = (key) => {
    setTabIndex(key)
    if(key === '0'){
      actionRef.current &&  actionRef.current.reload();
    }else{
      actionRef2.current && actionRef2.current.reload();
    }
  }

  return (
    <PageHeaderWrapper>
      <Tabs animated={false} defaultActiveKey="0" onChange={handleChangTabIndex}>
        <TabPane tab="未签署" key="0">
          <ProTable
            actionRef={actionRef}
            rowKey="oid"
            request={(params) => employeesList(params,tabIndex)}
            columns={columns}
          />
          <CreateForm
            record={record}
            loading={loading}
            visible={visible}
            onCancel={handleCancel}
            onSubmit={handleSubmit}
          />
        </TabPane>
        <TabPane tab="已签署" key="1">
          <ProTable
            actionRef={actionRef2}
            rowKey="oid"
            request={(params) => employeesList(params,tabIndex)}
            columns={alreadyColumns}
          />
        </TabPane>
      </Tabs>
    </PageHeaderWrapper>
  )
}

export default TableList
